<!-- 充电桩管理 -->
<template>
  <div class="home">
    <div class="detail_box">
      <!-- 操作 -->
      <div class="operate_box">
        <div class="item_box">
          <div class="operate_item">
            <label>关键词：</label>
            <el-input type="text" placeholder="请输入关键词" v-model="keyword"></el-input>
          </div>
          <div class="operate_item">
            <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
            <span class="operate_btn reset_btn" @click="resetTable">重置</span>
          </div>
        </div>
        <div class="btn_box" v-if="btn&&(btn.synchronize||btn.add)">
          <span class="operate_btn search_btn" @click="synchronizeData" v-if="btn.synchronize">同步数据</span>
          <span class="operate_btn search_btn" @click="openPopup('',1,0)" v-if="btn.add">新增</span>
        </div>
      </div>
      <!-- 表格 -->
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            prop="deviceId"
            label="控制器编号">
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称">
            <template slot-scope="scope">
              <p>{{ scope.row.name ? scope.row.name : '——' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="socketInfo"
            label="充电终端">
            <template slot-scope="scope">
              <p>{{ scope.row.socketInfo ? scope.row.socketInfo : '——' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="终端类型">
            <template slot-scope="scope">
              <p>{{ scope.row.socketType === 1 ? '单通道' : '双通道' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="typeName"
            label="告警类型">
            <template slot-scope="scope">
              <p>{{ scope.row.typeName ? scope.row.typeName : '——' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            sortable
            prop="createTime"
            label="告警时间">
            <template slot-scope="scope">
              <p>{{ formatTime(scope.row.createTime) }}</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="rssi"
            label="rssi信号">
            <template slot-scope="scope">
              <p>{{ scope.row.rssi ? scope.row.rssi : '——' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            width="80"
            label="在线状态">
            <template slot-scope="scope">
              <p class="normal" :class="{'unusual':scope.row.isOnline===0}">
                {{ scope.row.isOnline === 1 ? '在线' : '离线' }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            v-if="btn&&(btn.detail||btn.update||btn.bindMonitor||btn.delete||btn.add)"
            fixed="right"
            width="240">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="openPopup(scope.row,0)" v-if="btn.detail">详情</span>
                <span class="change_info" @click="openPopup(scope.row,1,1)" v-if="btn.update">编辑</span>
                <span class="change_info" @click="openPopup(scope.row,2)" v-if="btn.bindMonitor">绑定监控</span>
                <span class="change_info delete_btn" @click="deleteItem(scope.row.id)" v-if="btn.delete">删除</span>
                <span class="change_info" @click="openPopup(scope.row,1,0)" v-if="btn.add">复制</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="showType===0?'设备详情':deviceName+'绑定监控设备'" :visible.sync="isDetail" width="80%"
               @close='isDetail=false' append-to-body v-dialogDrag>
      <chargeDetail :deviceId="deviceId" v-if="isDetail&&showType===0"/>
      <bindMonitorDevice :chargeId="deviceId" v-if="isDetail&&showType===2"/>
    </el-dialog>
    <edit v-if="isPop"
          :deviceId="deviceId"
          :deviceLat="lat"
          :deviceLng="lng"
          :deviceName="deviceName"
          :url="address"
          :type="operateType"
          @listenToChild="listenToEdit"/>
  </div>
</template>

<script>
import chargeDetail from "./chargeDetail";
import bindMonitorDevice from "./bindMonitorDevice";
import edit from './edit'
import commons from "@/utils/common";
import {getBtn} from "@/utils/btnAuth";
import {deleteCharge, getCharge, synchronizeCharge} from "../../../api/device";

export default {
  components: {
    chargeDetail,
    bindMonitorDevice,
    edit
  },
  data() {
    return {
      keyword: '',
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      tableInfo: [],
      isDetail: false,
      deviceId: '',
      isPop: false,
      lng: '',
      lat: '',
      deviceName: '',
      address: '',
      showType: '',
      btn: {},
      operateType: 0
    }
  },
  mounted() {
    getBtn().then((res=>{
      this.btn = res;
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    }))
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
        keyword: that.keyword,
      }
      getCharge(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    // 重置
    resetTable() {
      this.pageSize = 15;
      this.keyword = '';
      this.getTableInfo(1)
    },

    openPopup(obj, type, operate) {
      type === 1 ? this.isPop = true : this.isDetail = true;
      this.deviceId = type === 0 || operate === 0 ? obj.deviceId : obj.id;
      this.showType = type;
      this.deviceName = obj.name ? obj.name : '';
      this.operateType = operate ? operate : 0
      if (type === 1) {
        this.lng = obj.lng ? obj.lng : '';
        this.lat = obj.lat ? obj.lat : '';
        this.address = obj.url ? obj.url : '';
      }
    },

    listenToEdit(e) {
      this.isPop = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage);
      }
    },

    deleteItem(id) {
      let that = this;
      that.$confirm('确定删除设备信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.loading = true;
        deleteCharge(id).then(res => {
          if (res.success) {
            that.getTableInfo(that.currentPage);
            that.$message.success('删除成功！');
          }
          that.loading = false;
        }, (err) => {
          that.loading = false;
          that.$message.error('删除失败，请重试');
        });
      }).catch(() => {
        // console.log('fail')
      });
    },

    // 同步数据
    synchronizeData() {
      this.loading = true;
      synchronizeCharge().then(res => {
        if (res.success) {
          this.getTableInfo(this.currentPage)
        }
        this.loading = false;
      }, (err) => {
        this.loading = false;
      });
    },

    formatTime(time) {
      return commons.formatTime(time)
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
